﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DrivingGame
{
    class Computer : Car
    {
        // Collision detectors will be used to help computer car make judgement of how to move.
        private BoundingSphere[] collisionDetectors;
        private float detectorSphereRadius = 0.5f;
        private float angleBetweenTwoDectors = MathHelper.Pi/5;

        public Computer()
        {
            collisionDetectors = new BoundingSphere[5];
            this.topSpeeds = new float[5] { 0.0f, 15.0f, 25.0f, 35.0f, 45.0f };
            this.accelerations = new float[5] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
        }

        public void Move(GameTime gameTime)
        {
            updateCollisionDetectors();  

            if (CollidesWithBuildings(collisionDetectors[0]))
            {
                Free();
                NoTurn();

                if (!CollidesWithBuildings(collisionDetectors[1]))
                {
                    Right();
                    Brake();
                }
                else
                {
                    if (!CollidesWithBuildings(collisionDetectors[2]))
                    {
                        Left();
                        Brake();
                    }
                    else
                    {
                        Brake();
                    }
                }
            } 
            else 
            {
                
                if (CollidesWithBuildings(collisionDetectors[4]))
                {
                    Right();
                    Free();
                }

                if (CollidesWithBuildings(collisionDetectors[3]))
                {
                    Left();
                    Free();
                }

                if (!CollidesWithBuildings(collisionDetectors[3]) && !CollidesWithBuildings(collisionDetectors[4]))
                {
                    Accelerate();
                    NoTurn();
                }
            }
        }

        public void updateCollisionDetectors()
        {
            Vector3 detectorPosition;
            Quaternion detectorRotation;
            float dist;
            Vector3 addVector;

            int index = 0;
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    dist = Math.Abs(speed) * 1.5f;
                    detectorPosition = this.position;
                    detectorRotation = this.rotation * Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0), i * angleBetweenTwoDectors);
                    addVector = Vector3.Transform(new Vector3(0, 0, 1), detectorRotation);
                    detectorPosition += addVector * dist;
                    this.collisionDetectors[index] = new BoundingSphere(detectorPosition, detectorSphereRadius);
                    index++;
                }
                else
                {
                    if (i == 1)
                        dist = 20.0f;
                    else
                        dist = 10.0f;
                    detectorPosition = this.position;
                    detectorRotation = this.rotation * Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0), i * angleBetweenTwoDectors);
                    addVector = Vector3.Transform(new Vector3(0, 0, 1), detectorRotation);
                    detectorPosition += addVector * dist;
                    this.collisionDetectors[index] = new BoundingSphere(detectorPosition, detectorSphereRadius);
                    index++;

                    detectorPosition = this.position;
                    detectorRotation = this.rotation * Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0), -i * angleBetweenTwoDectors);
                    addVector = Vector3.Transform(new Vector3(0, 0, 1), detectorRotation);
                    detectorPosition += addVector * dist;
                    this.collisionDetectors[index] = new BoundingSphere(detectorPosition, detectorSphereRadius);
                    index++;
                }
            }
        }

        public void DrawDetectors(Matrix view, Matrix projection)
        {
            Color[] colors = new Color[5] {Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Black};
            for (int i = 0; i < collisionDetectors.Length; i++)
            {
                BoundingSphereRenderer.Render(collisionDetectors[i], view, projection, colors[i]);
            }
            
        }
    }
}
